package com.example.sharesinfo.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sharesinfo.MainActivity;
import com.example.sharesinfo.R;
import com.example.sharesinfo.base.BaseUIActivity;
import com.example.sharesinfo.bean.FocusBean;
import com.example.sharesinfo.bean.PersonalCenterBean;
import com.example.sharesinfo.bean.UserBean;
import com.example.sharesinfo.entity.Constants;
import com.example.sharesinfo.entity.User;
import com.example.sharesinfo.helper.UIHelper;
import com.example.sharesinfo.model.FocusModel;
import com.example.sharesinfo.utils.CommonUtils;
import com.example.sharesinfo.utils.LogUtils;
import com.example.sharesinfo.utils.OkHttpUtils;
import com.example.sharesinfo.utils.SpUtils;
import com.example.sharesinfo.utils.TimeUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class LoginActivity extends BaseUIActivity implements View.OnClickListener {
    private User user = new User();
    private String ToastMessage = "登录成功";
    private ImageButton mReturnBtn;
    private ImageButton mGoToRegister;
    private TextView mLoginText;
    private EditText mUsername;
    private EditText mPassword;
    private Button mLoginBtn;

    //Okhttp的请求参数
    private String urlRoot = "http://119.23.221.142/api/user/Login?";

    @SuppressLint("HandlerLeak")
    private final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            LogUtils.i("handleMessage" + msg.what);
            switch (msg.what) {
                case 200:
                    UserBean result = (UserBean) msg.obj;
                    SpUtils.getInstance().putString(Constants.SP_USER_ID, String.valueOf(result.getData().getId()));
                    SpUtils.getInstance().putString(Constants.SP_USER_NAME, result.getData().getUsername());
                    SpUtils.getInstance().putBoolean(Constants.SP_IS_FIRST_APP, false);
                    SpUtils.getInstance().putString(Constants.SP_USER_IMAGE_URL, result.getData().getImageUrl());
                    SpUtils.getInstance().putBoolean(Constants.SP_IS_LOGIN, true);
                    UIHelper.hideDialogForLoading();
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    finish();
                    break;
                case 402:
                    UIHelper.hideDialogForLoading();
                    Toast.makeText(LoginActivity.this, msg.obj.toString(), Toast.LENGTH_SHORT).show();
                    break;
                case 404:
                    break;
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void isLogin() {
        boolean isLogin = SpUtils.getInstance().getBoolean(Constants.SP_IS_LOGIN, false);
        if (isLogin) {
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
            finish();
        }
    }

    private void initView() {
        mReturnBtn = findViewById(R.id.return_btn);
        mGoToRegister = findViewById(R.id.go_to_register);
        mLoginText = findViewById(R.id.login_text);
        mUsername = findViewById(R.id.Username);
        mPassword = findViewById(R.id.Password);
        mLoginBtn = findViewById(R.id.login_btn);

        mLoginBtn.setOnClickListener(this);
        mGoToRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.login_btn:
                UIHelper.showDialogForLoading(this, "登录中");
                doLogin(mUsername.getText().toString(), mPassword.getText().toString());
                break;
            case R.id.go_to_register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
        }
    }

    private void doLogin(String Phonenumber, String Password) {
        //这里是拼接的请求头
        //Map<String, String> headersdatas = new HashMap<String, String>();
        //这里是拼接的请求参数
        Map<String, String> datas = new HashMap<String, String>();
        //这里是post请求参数
        //Map<String, String> BodyParams = new HashMap<String, String>();
        datas.put("Phonenumber", Phonenumber);
        datas.put("Password", Password);
        OkHttpUtils.getInstance().Post(urlRoot, null, datas,
                null, null, new OkHttpUtils.okHttpListener() {
                    Message mess = mHandler.obtainMessage();

                    @Override
                    public void OnSuccess(String s, int code) throws JSONException {
                        JSONObject object = new JSONObject(s);
                        if (object.getString("code").equals("200")) {
                            UserBean userBean = new Gson().fromJson(s, UserBean.class);
                            mess.what = code;
                            mess.obj = userBean;
                            mHandler.sendMessage(mess);
                        } else {
                            mess.what = 402;
                            mess.obj = object.getJSONArray("data").
                                    getJSONObject(0).getString("Message");
                            mHandler.sendMessage(mess);
                        }


                    }

                    @Override
                    public void OnFailed(String s) {

                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        String userName = SpUtils.getInstance().getString(Constants.SP_USER_NAME, "");
        String passWord = SpUtils.getInstance().getString(Constants.SP_USER_PASSWOED, "");
        if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(passWord)) {
            mUsername.setText(userName);
            mPassword.setText(passWord);
        }

    }
}
